package com.xing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xing.Utils.Result;
import com.xing.config.JSONUtils;
import com.xing.core.BaseServiceImpl;
import com.xing.dao.ViewHistoryDao;
import com.xing.dto.ViewHistoryDTO;
import com.xing.entity.ViewHistoryEntity;
import com.xing.feign.RoomFeign;
import com.xing.feign.RoomTagFeign;
import com.xing.feign.UserFeign;
import com.xing.feign.dto.LiveRoomDTO;
import com.xing.feign.dto.SysUserDTO;
import com.xing.service.ViewHistryService;
import com.xing.user.User;
import com.xing.utils.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @version 1.0
 * @Author Xing
 * @Date 2024/4/27 16:53
 */
@Service
@Slf4j
public class ViewHistryServiceImpl extends BaseServiceImpl<ViewHistoryDao, ViewHistoryEntity> implements ViewHistryService {
    @Autowired
    private ViewHistoryDao dao;
    @Autowired
    private RoomFeign roomFeign;
    @Autowired
    private UserFeign userFeign;

    @Override
    public PageData<ViewHistoryDTO> GetViewHistory(Integer page, Integer limit) {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<ViewHistoryDTO> viewHistoryList = dao.getViewHistoryList(principal.getId(), (page - 1) * limit, limit);
        for (ViewHistoryDTO dto : viewHistoryList) {
            Result room = roomFeign.getLiveRoomByRoomNumber(dto.getRoomId());
            LiveRoomDTO roomDTO = JSONUtils.objParse(room.getData(), LiveRoomDTO.class);
            Result userById = userFeign.getUserById(String.valueOf(roomDTO.getAnchorId()));
            SysUserDTO user = JSONUtils.objParse(userById.getData(), SysUserDTO.class);
            log.info("user:{}", user);
            dto.setUrl(roomDTO.getUrl());
            dto.setNickname(user.getNickname());
            dto.setInfo(roomDTO.getInfo());
            dto.setHeadUrl(user.getUrl());
        }
        return new PageData<>(viewHistoryList, dao.getViewHistoryListNum(principal.getId()));
    }

    @Override
    public void DeleteViewHistory(Long id) {
        ViewHistoryEntity viewHistoryEntity = dao.selectById(id);
        viewHistoryEntity.setDelFlag(1);
        super.updateById(viewHistoryEntity);
    }

    @Override
    public void updateViewHistory(Long roomId, Long userId) {
        QueryWrapper<ViewHistoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", roomId);
        wrapper.eq("user_id", userId);
        wrapper.eq("del_flag", 0);
        Long l = dao.selectCount(wrapper);
        if (l == 0) {
            ViewHistoryEntity viewHistoryEntity = new ViewHistoryEntity();
            viewHistoryEntity.setRoomId(roomId);
            viewHistoryEntity.setUserId(userId);
            viewHistoryEntity.setViewDate(new Date());
            viewHistoryEntity.setDelFlag(0);
            dao.insert(viewHistoryEntity);
        } else {
            ViewHistoryEntity viewHistoryEntity = dao.selectOne(wrapper);
            dao.updateById(viewHistoryEntity);
        }
    }
}
